<?php
include 'islogin.php';
include '../config.php';
	
if( isset($_GET['flag']))
{
	if($_GET['flag']=="update")
	{
		$sql = "select a.id,a.name,a.model,a.tao_link,a.type_id,a.price,a.path,a.remark,a.content,a.dates,a.ishot,a.isnews from en_product as a where a.flag =1 and a.id=".$_GET['id'];
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
	}
}
if(isset($_POST['flag']))
{
   if(!empty($_POST['pro_type']))
    {
        $type_id = implode(',', $_POST['pro_type']);
    }
    else
    {
        $type_id = '';
    }
	
	if($_POST['flag']=="update")
	{
		$sql = "update en_product set name='".$_POST['name']."', model='".$_POST['model']."', type_id ='".$type_id."',price =".$_POST['price']." ,path = '".$_POST['path']."',remark = '".$_POST['remark']."',content = '".$_POST['content']."',dates='".$_POST['dates']."',ishot= ".$_POST['ishot'].",isnews= ".$_POST['isnews'].", tao_link='".$_POST['tao_link']."' where id =".$_POST['id'];
		mysql_query($sql);
		echo "<script>window.location.href='product_list.php'</script>";
	}
	if($_POST['flag']=="add")
	{
		$sql = "insert into en_product(name,model,type_id,price,path,remark,content,dates,ishot,tao_link,isnews,flag) values('".$_POST['name']."','".$_POST['model']."','".$type_id."', ".$_POST['price'].",'".$_POST['path']."','".$_POST['remark']."','".$_POST['content']."','".$_POST['dates']."',".$_POST['ishot'].",'".$_POST['tao_link']."',".$_POST['isnews'].",1)";
		
		mysql_query($sql);
		echo "<script>window.location.href='product_list.php'</script>";
	}
	
}
?>
<script src="../js/jquery-1.7.1.js"></script>
<script type="text/javascript" src="js/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="js/ckfinder/ckfinder.js"></script>
<link href="images/skin.css" rel="stylesheet" type="text/css" />
<link href="css/right.css" rel="stylesheet" type="text/css"/>
<link href="js/upload/uploadify.css" rel="stylesheet" type="text/css" />
<script src="js/upload/swfobject.js"></script>
<script src="js/upload/jquery.uploadify.v2.1.4.min.js"></script>
<style>
   #img_uploadUploader{vertical-align:middle}
</style>
<script>
	$(document).ready(function(){

		   $('#img_upload').uploadify({
			   'uploader':'js/upload/uploadify.swf',
			   'cancelImg':'js/upload/cancel.png',
			   'fileExt':'*.png',
			   'script':'med_upload.php',
			   'auto':true,
			   'folder':'upload',
			   'buttonImg':'images/Browse.png',
			   'width':70,
			   'height':25,
			   'wmode':'transparent',
			   'onComplete':function(event,queueId,fileObj,response,data){
				   	 
				     $("#path").val(response);
				   }
	     });

	       $('#save_bt').click(function(){
	            if($('#form1').find(':checkbox[name="pro_type[]"][checked="checked"]').length)
	            {
	                $('#form1').submit();
	            }
	            else
	            {
	                alert("请至少选择一个分类");
	            }
	        });
		});
</script>
<div style="border:0px solid red;"> 

    	<div id="top"><div>产品管理</div></div>
    	
	    <div id="middle">
	     <div class="layout">
		  	<div class="title">产品管理</div>
		       <div class="content">
			       <form action="product_add.php" id="form1"  method="post">
			         <input type="hidden" name="flag" value="<?php if(isset($row)){echo "update";}else{echo "add";}?>" />
			         <input type="hidden" name="id" value="<?php if(isset($row)){echo $row['id'];}?>" />
			          <table  cellspacing="0" cellpadding="0">  
			          	<tr>
			          		<td class="title_some_td" style="width:100px;">产品名称:</td>
			          		<td><input name="name" value="<?php if(isset($row)){echo $row['name'];}?>"  /></td>
			          	</tr>
			          	<tr>
			          		<td class="title_some_td" style="width:100px;">产品型号:</td>
			          		<td><input name="model" value="<?php if(isset($row)){echo $row['model'];}?>"  /></td>
			          	</tr>
			          	<tr>
			          		<td class="title_some_td" style="width:100px;">产品类型:</td>
			          		<td>
			          		    <?php
                                  if(isset($row['type_id']))
                                    $pro_type_arr = explode(',' ,$row['type_id']);
                                  else
                                    $pro_type_arr = array();

                                  $sql_t = "select * from en_pro_type where flag = 1";
                                  $ret1 = mysql_query($sql_t);
                                  while($row1 = mysql_fetch_array($ret1)){
                                ?>
                                <label style="margin-left:10px; font-size:12px;"><?php echo $row1['name'];?></label><input <?php if(in_array($row1['id'], $pro_type_arr)){ echo "checked='checked'";}?> type="checkbox" name="pro_type[]" value="<?php echo $row1['id'];?>" />
                                <?php }?>
			          		</td>
			          	</tr>
			          	<tr>
			          		<td class="title_some_td" style="width:100px;">产品价格:</td>
			          		<td><input name="price" value="<?php if(isset($row)){echo $row['price'];}?>"  /></td>
			          	</tr>
			          	
			          	<tr>
			          		<td class="title_some_td" style="width:100px;">图片:</td>
			          		<td><input id="path" name="path" value="<?php if(isset($row)){echo $row['path'];}?>"  /> <input id="img_upload" /></td>
			          	</tr>
			          	
			          	<tr>
			          		<td class="title_some_td" style="width:100px;">产品概要:</td>
			          		<td><input name="remark" value="<?php if(isset($row)){echo $row['remark'];}?>"  /></td>
			          	</tr>
			          	
			          	<tr>
			          		<td class="title_some_td" style="width:100px;">上市时间:</td>
			          		<td><input name="dates" value="<?php if(isset($row)){echo $row['dates'];}?>"  /><span>格式:2012-02-02</span></td>
			          	</tr>
			          	
			          	<tr>
                            <td class="title_some_td" style="width:100px;">淘宝链接:</td>
                            <td><input name="tao_link" value="<?php if(isset($row)){echo $row['tao_link'];}?>"  /><span>必须加上http://</span></td>
                        </tr>
			          	
			          	<tr>
			          		<td class="title_some_td" style="width:100px;">热销产品:</td>
			          		<td>
			          			<?php 
			          				if(isset($row)){
			          					if($row['ishot']==1)
			          					{
			          						$hot = "selected";
			          					}
			          				}
			          				
			          			?>
			          			<select name="ishot" >
			          				<option value="0">否</option>
			          				<option value="1" <?php if(isset($hot)){echo $hot;}?>>是</option>
			          			</select>
			          		</td>
			          	</tr>
			          	
			          	<tr>
			          		<td class="title_some_td" style="width:100px;">最新产品:</td>
			          		<td>
			          			<?php 
			          				if(isset($row)){
			          					if($row['isnews']==1)
			          					{
			          						$news = "selected";
			          					}
			          				}
			          				
			          			?>
			          			<select name="isnews" >
			          				<option value="0">否</option>
			          				<option value="1" <?php if(isset($news)){echo $news;}?>>是</option>
			          			</select>
			          		</td>
			          	</tr>
			          	
			          	<tr>
			          		<td class="title_some_td" style="width:100px;">产品介绍:</td>
			          		<td>
			          			<textarea name="content" id="content"><?php if(isset($row)){echo $row['content'];} ?></textarea>
			          		</td>
			          		<script type="text/javascript">
									var editor =CKEDITOR.replace( 'content',
									{
										skin : 'kama',
										filebrowserImageUploadUrl : 'js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images'
									});
									
						</script>
			          	</tr>
			          	
			          	
			          	
			          	<tr>
			          		<td></td>
			          		<td><input type="button" id="save_bt" value="保存" /></td>
			          	</tr>
			          </table>
			      </form>    
		      </div>
		  </div>
	    </div>
	    
	    <div id="bottom"></div>		
</div>	    